$(function() {
	$("#searchid").linkbutton({
		 text:'查询',
		 iconCls:'icon-search'
	});
	$('#pinggou').datagrid({
		title : '您所在的位置：拼购管理模块&nbsp;&nbsp;--&gt;&gt;&nbsp;&nbsp;拼购设置',
		idField : 'id',
		nowrap : false,
		striped : true,
		collapsible : false,
		width : 1000,
		height : 500,
		url : '../tpinggou/search.shtml',
		columns : [ [{
					field : 'name',
					title : '拼购品牌',
					mapping : 'img_name',
					align:'center',
					width : 180
				},{
					field : 'imgsrc',
					title : '拼购商标',
					align:'center',
					width : 160,
					formatter:function(value){
						return '<img src="../'+value+'" width="144px" height="45px"/>';
					}
				}
				,{
					field : 'companyName',
					title : '公司名字',
					align:'center',
					width : 180
				}
				,{
					field : 'accTitle',
					title : '所在类别',
					align:'center',
					width : 200
				},{
					field : 'auding',
					title : '权重',
					align:'center',
					width : 60
				}
				, {
					field : 'id',
					title : '操作',
					align : 'center',
					width : 150,
					formatter : function(value, rec) {
						var id = rec.id;
						var weights = rec['auding'];
						var categoryId = rec['categoryId'];
						var typeId = rec['typeId'];
						if (rec.id == undefined)
							return null;
						else
							return '<span style="color:red" id="operate"><a href="javascript:modifyWeight(\''+weights+'\',\''+id+'\',\''+categoryId+'\',\''+typeId+'\')">修改权重</a>&nbsp;&nbsp;<a href="javascript:Ondelete(\''+id+'\',\''+categoryId+'\',\''+typeId+'\')">删除</a></span>';
					}
				}

		]],
		pagination : true,
		pageList:[10,20,50,80,100],
		rownumbers : true,
		singleSelect:true,
		toolbar : [{
					id : 'btnadd',
					text : '新增拼购信息',
					iconCls : 'icon-add',
					handler : function() {
						$(this).attr("href", '../tpinggou/goForwordPage.shtml');

					}
				},'-',{
					id : 'btnclose',
					text : '  &nbsp;关  闭',
					iconCls : 'icon-cancel',
					handler : function() {
						window.self.close();
					}
				}]
	});
	
	$("#modifyWindow").dialog({
		title:'修改',
		width:300,
		height:120,
		resizable:false,
		minimizable:false,
		maximizable:false,
		toolbar: [{
			text : '保存信息',
		    iconCls:'icon-save',
		    handler:function(){
		    		var categoryId = $("#categoryIdw").val();
					var typeId = $('#typeIdw').val();
						var url1 = "../tpinggou/modifyWeights.shtml";
						var weights = $("#weightValue").val();
						if(isNaN(weights)){
							alert("权重值只能为数字");
							return;
						}
						var brandId = $("#brandIdValue").val();
						$.ajax({
							url : url1,
							data : "brandId=" + brandId+"&categoryId="+categoryId+"&weights="+weights+'&typeId='+typeId,
							success : function(msg) {
								if(msg=="1"){
									alert("保存成功");							
								}else{
									alert("保存失败");
								}
									$("#modifyWindow").dialog('close');
								$('#pinggou').datagrid("reload");
							}
						});
		    }
		  }],
		closed:true
	})
	
	
});



function Ondelete(id,categoryId,typeId) {
						var url1 = "../tpinggou/deletePingGou.shtml";
						$.ajax({
									url : url1,
									data : "deleteId=" + id+"_&categoryId="+categoryId+"&typeId="+typeId ,
									success : function(msg) {
										if(msg == '1'){
											alert("删除成功");
											$('#pinggou').datagrid("reload");
										}else if(msg == "3"){
											alert("该拼购信息已经设置成首页推荐，禁止删除");
										}
										else{
											alert("删除失败,请联系管理员");
										}
									}
								});
	}
function Onmodify(id) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	window.location.href = "../tpNewpic/searchById.shtml?id=" + id ;
}

function bigCategoryChange(){
	var bigCategoryValue = $("#bigCategory option:selected").val();
	if(bigCategoryValue != ""){
		$('#smallCategory').html('加载中... ...');
		$('#goodsType').html('');
		$.get('../category/category_loadCategoryByPid.shtml?id='+bigCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="categoryId" id="categoryId" onchange="smallCategoryChange();" ><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			
			var str1 = '<select name="typeId" id="typeId" ><option value="">请选择</option>';
			str1+='</select>';
			$('#goodsType').html(str1);
			
			$('#smallCategory').html(str);
		})
	}
}

function smallCategoryChange(){
	var smallCategoryValue = $("#categoryId option:selected").val();
	if(smallCategoryValue != ""){
		$('#goodsType').html('加载中... ...');
		$.get('../type/type_loadTypeByCategoryId.shtml?categoryId='+smallCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="typeId" id="typeId" ><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#goodsType').html(str);
		})
	}
	
}

function onSearch(){
	var categoryId = $('#categoryId option:selected').val();
	var typeId = $('#typeId option:selected').val();
	var queryParams =$('#pinggou').datagrid("options").queryParams;
	var bigCategory = $('#bigCategory option:selected').val();
	queryParams.categoryId=categoryId;
	queryParams.typeId=typeId;
	queryParams.bigCategory=bigCategory;
	queryParams.name=$("#name").val();
	$('#pinggou').datagrid("reload");
}
function modifyWeight(weights,brandId,categoryId,typeId) {
	$("#weightValue").val(weights);
	$("#brandIdValue").val(brandId);
	$("#modifyWindow").window('open');
	$("#categoryIdw").val(categoryId);
	$("#typeIdw").val(typeId);
}
